package edu.csumb.itcd.teammario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;

public class PoisonEnemy extends Enemy{

    private int sightDistance = 0;
    private int rateOfFire;
    private int duration;
    
    public PoisonEnemy(Point spawnPoint) {
        super(spawnPoint);
        
        sightDistance = 600;
        damage = 1;
        rateOfFire = 10;
        duration = 3;
        width = 15;
        height = 15;
        weapon = new PoisonWeapon(damage, rateOfFire, duration);
        weapon.setWeaponDamage(1);
        weapon.setRateOfFire(20);
        // TODO Auto-generated constructor stub
    }
    
    public void moveAi(int playerX, int playerY)
    {

    	 // Check to see if player is in lineOfSight
        Ellipse2D lineOfSight = new Ellipse2D.Double(enemyPosition.getX() - ((width + sightDistance)/2), enemyPosition.getY() - ((height + sightDistance)/2), width + sightDistance, height + sightDistance);
        if (lineOfSight.intersects(Player.getPlayerPosition().x, Player.getPlayerPosition().y, Player.getWidth(), Player.getHeight()) && visible)
        {
            //Shoot player
            
            weapon.enemyFire((Point)enemyPosition, Player.getPlayerPosition());    
            if (currentHealth <= 0)
            {
                isAlive = false;
                LeaderBoard.incrementKillCount();
            }
        }
        else
        {
           
        	vectorY = playerY - (height/2) + (Player.getHeight()/2) - enemyPosition.getY();
            vectorX = playerX - (width/2) + (Player.getWidth()/2) - enemyPosition.getX();

            //The dist variables need to be positive for the trig equations.
            distY = Math.abs(vectorY);
            distX = Math.abs(vectorX);

            //Gives the vectors a value of 1 or -1.
            vectorY /= distY;
            vectorX /= distX;


            if (distX == 0 && distY == 0)
            {
                moveY = 0;
                moveX = 0;
            } else if (distX == 0)
            {
            	moveY = speed * vectorY;
            	moveX = 0;
            } else if (distY == 0)
            {
            	moveY = 0;
            	moveX = speed * vectorX;
            } else
            {
                moveY = speed * Math.sin(Math.atan(distY/distX));
                moveX = speed * Math.cos(Math.atan(distY/distX));
//                moveY = Math.rint(moveY) * vectorY;
//                moveX = Math.rint(moveX) * vectorX;
                moveY = moveY * vectorY;
                moveX = moveX * vectorX;
            }
            
            enemyPosition.setLocation(enemyPosition.getX() + moveX, enemyPosition.getY() + moveY);
            if (getCurrentHealth() <= 0)
            {
                isAlive = false;
                LeaderBoard.incrementKillCount();
            }
        }
        
        if (enemyPosition.getX() > 0 && enemyPosition.getX() + width < 800 - 5 && enemyPosition.getY() > 55 && enemyPosition.getY() + height < 600 - 30)
        {
        	visible = true;
        }
        
    }
    public int getCurrentHealth()
    {
        return currentHealth;
    }
    public void setCurrentHealth(int health)
    {
        currentHealth = health;
    }
    public boolean isActive()
    {
        return isAlive;
    }
    
    public void paintEnemy(Graphics2D g2)
    {
    	if (isAlive == true)
        {
            //temp is to make sure the number is in bounds for the new Color
            int temp = (currentHealth*300)/40;
            if (temp < 0)
                temp = 0;
            else if (temp > 255)
                temp = 255;
            
            enemyc = new Color(0,255,temp);
            g2.setColor(Color.black);
        	g2.drawRect((int)enemyPosition.getX() -1,(int)enemyPosition.getY() -1 ,width + 1, height + 1);
           

            if (poisonReceived.isCurrentlyPoisoned() == true)
            {
            	g2.setColor(Color.GREEN);
            }
            else
            {
            	g2.setColor(enemyc);
            }
            g2.fillRect((int)enemyPosition.getX(), (int)enemyPosition.getY(), width, height);    
        }
    }
}

